<template>
  <div>
    <nut-tabs background="#fff" type="smile" swipeable v-model="currentMenu">
      <nut-tab-pane v-for="(type, index) in evaluationType" :key="index"
        :customStyle="{ background: colors.bgColor, padding: ' 0' }" :title="type.label">
        <div v-if="isShow">
          <!-- 待评价 -->
          <div v-if="currentMenu == 0" bg-white p-24rpx my-10rpx v-for="(item, i) in currentData.list" :key="i" @click="linkMsgEdit(item)">
            <div text-28rpx>{{item.title}}</div>
            <div text-26rpx text-neutral-400 my-14rpx>{{item.content}}</div>
            <div text-24rpx text-neutral-500>{{item.createTime}}</div>
          </div>
          <!-- 已评价 -->
          <div v-if="currentMenu == 1" bg-white p-24rpx my-10rpx v-for="(item, i) in currentData.list" :key="i">
            <div text-28rpx>{{item.title}}</div>
            <div text-26rpx text-neutral-400 my-14rpx>{{item.content}}</div>
            <div text-24rpx text-neutral-500>{{item.createTime}}</div>
          </div>

          <div v-if="!currentData.list.length" mt-100rpx>
            <empty />
          </div>
        </div>
      </nut-tab-pane>
    </nut-tabs>

  </div>
</template>

<script setup lang="ts">
import { evaluation, getComments, getOrderList } from '@/api/order';
import order from '@/components/basic-template/order.vue';
import empty from '@/components/empty/index.vue';
import { colors } from '@/constants/injection-key';
import { onPullDownRefresh, onReachBottom, onShow } from '@dcloudio/uni-app';
import type { Search } from '~/constants/types';
import { messages, editMessages } from "@/api/message";

const isShow = ref<boolean>(false)
const loading = ref<boolean>(false)
const evaluationType = ref<any>([
  {label: "未读",},
  {label: "已读",},
])
const system = ref<any>('')
const currentMenu = ref<number>(0)
watch(() => currentMenu.value, (val, oldVal) => {
  init()
})
const currentData = ref<any>({
  list: [],
  data: "",
})

const searchForm = ref<Search>({
  pageSize: 10,
  pageNumber: 1,
  memberId: "",
  messageId: "",
  status:"UN_READY"
})

async function init() {
  isShow.value = false
  currentData.value.list = []
  currentData.value.pageNumber = 1
  currentData.value.pageSize = 10
  searchForm.value.pageNumber = 1;
  await fetch()

}
// 查询站内消息
async function fetch() {
  uni.stopPullDownRefresh();
  if (currentMenu.value == 1) {
    searchForm.value.status = "ALREADY_READY";
  } else {
    searchForm.value.status = "UN_READY";
  }
  let res = await messages(searchForm.value);
  isShow.value = true
  if (res.data.success) {
    currentData.value.data = res.data.result;
    let lists = [] as any;
    res.data.result.records.forEach((item: any) => {
    	lists.push(item);
    	let obj = {} as any;
      //对象去重
    	lists = lists.reduce((cur: any, next: any) => {if (next.id != undefined) {obj[next.id] ?"" :(obj[next.id] = true && cur.push(next));}return cur;},[])
    })
    currentData.value.list.push(...lists);
  }
}
// 消息设置为已读
async function linkMsgEdit(v: any) {
  let params = { messageId: v.memberId };
  editMessages(v.id, params).then(res => {
    if (res.data.success) {
      // currentData.value.list.forEach((item: any,index: any)=>{
      //     if(item.id == v.id){
      //       currentData.value.list.splice(index, 1)
      //     }
      // })
      init();
      uni.showToast({
        title: '消息已读',
        icon: "none"
      })
    }
  })
}

onPullDownRefresh(() => {
  init()
})
onReachBottom(() => {
  if (currentData.value.data?.total > currentData.value.pageNumber * currentData.value.pageSize) {
    currentData.value.pageNumber++;
    searchForm.value.pageNumber++;
    fetch()
  }
})

onShow(async () => {
  currentData.value.pageNumber = 1;
  init();
  system.value = await uni.getSystemInfo();
})
</script>

<style scoped></style>
